﻿using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows;
using BingleMaps.Services;
using BingleMaps.ViewModels;

namespace BingleMaps.Views
{
	public partial class DropBoxView
	{
		private readonly SettingsViewModel _settings;
		private readonly ObservableCollection<FrameworkElement> _navHistory = new ObservableCollection<FrameworkElement>();

		public DropBoxView()
		{
			InitializeComponent();
			_settings = (SettingsViewModel)DataContext;
			_navHistory.CollectionChanged += (s, e) =>
			{
				switch (e.Action)
				{
					case NotifyCollectionChangedAction.Add:
						if (_navHistory.Count > 1)
						{
							_navHistory[_navHistory.Count - 2].Visibility = Visibility.Collapsed;
						}
						((FrameworkElement)e.NewItems[0]).Visibility = Visibility.Visible;
						break;
					case NotifyCollectionChangedAction.Remove:
						((FrameworkElement)e.OldItems[0]).Visibility = Visibility.Collapsed;
						_navHistory[_navHistory.Count - 1].Visibility = Visibility.Visible;
						break;
				}
			};
			_navHistory.Add(HomePanel);
		}

		protected override void OnBackKeyPress(CancelEventArgs e)
		{
			if (NavigationService != null && NavigationService.CanGoBack)
			{
				base.OnBackKeyPress(e);
			}
			else
			{
				e.Cancel = true;
			}
		}

		private void GoToLoginButtonClick(object sender, RoutedEventArgs e)
		{
			_navHistory.Add(LoginPanel);
		}

		private void GoToRegisterButtonClick(object sender, RoutedEventArgs e)
		{
			_navHistory.Add(RegisterPanel);
		}

		private void LoginButtonClick(object sender, RoutedEventArgs e)
		{
			var isValid = true;

			LoginEmailErrorTextBlock.Visibility = LoginPasswordErrorTextBlock.Visibility = Visibility.Collapsed;
			if (LoginEmailTextBox.Text.Length == 0 || !_emailRegex.IsMatch(LoginEmailTextBox.Text))
			{
				LoginEmailErrorTextBlock.Visibility = Visibility.Visible;
				isValid = false;
			}
			if (LoginPasswordBox.Password.Length < 6)
			{
				LoginPasswordErrorTextBlock.Visibility = Visibility.Visible;
				isValid = false;
			}
			if (!isValid)
			{
				return;
			}
			ProgressBar.IsIndeterminate = true;
			DropBoxServices.Login(LoginEmailTextBox.Text, LoginPasswordBox.Password,
								  login =>
								  {
									  ProgressBar.IsIndeterminate = false;
									  _settings.DropNetUserLogin = login;
									  _settings.UserLoginName = LoginEmailTextBox.Text;
									  NavigationService.GoBack();
								  },
								  ex =>
								  {
									  ProgressBar.IsIndeterminate = false;
									  MessageBox.Show("There was a problem logging in to your DropBox account. Please check that the email/password are correct and try again.", "Login Failed", MessageBoxButton.OK);
								  });
		}

		private readonly Regex _emailRegex = new Regex(@"^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$");

		private void RegisterButtonClick(object sender, RoutedEventArgs e)
		{
			var isValid = true;

			RegisterEmailErrorTextBlock.Visibility =
				RegisterPasswordErrorTextBlock.Visibility =
				ConfirmPasswordErrorTextBlock.Visibility =
				FirstNameErrorTextBlock.Visibility =
				LastNameErrorTextBlock.Visibility = Visibility.Collapsed;

			if (RegisterEmailTextBox.Text.Length == 0 || !_emailRegex.IsMatch(RegisterEmailTextBox.Text))
			{
				RegisterEmailErrorTextBlock.Visibility = Visibility.Visible;
				isValid = false;
			}

			if (RegisterPasswordBox.Password.Length < 6)
			{
				RegisterPasswordErrorTextBlock.Visibility = Visibility.Visible;
				isValid = false;
			}

			if (ConfirmPasswordBox.Password != RegisterPasswordBox.Password)
			{
				ConfirmPasswordErrorTextBlock.Visibility = Visibility.Visible;
				isValid = false;
			}

			if (FirstNameTextBox.Text.Length == 0)
			{
				FirstNameErrorTextBlock.Visibility = Visibility.Visible;
				isValid = false;
			}

			if (LastNameTextBox.Text.Length == 0)
			{
				LastNameErrorTextBlock.Visibility = Visibility.Visible;
				isValid = false;
			}

			if (!isValid)
			{
				return;
			}

			ProgressBar.IsIndeterminate = true;
			DropBoxServices.CreateAccount(RegisterEmailTextBox.Text, FirstNameTextBox.Text, LastNameTextBox.Text, RegisterPasswordBox.Password,
										  rsp =>
										  {
											  ProgressBar.IsIndeterminate = false;
											  return;
										  },
										  ex =>
										  {
											  ProgressBar.IsIndeterminate = false;
											  MessageBox.Show("There was a problem creating your DropBox account, please try again later.", "Register Failed", MessageBoxButton.OK);
										  });
		}

		private void PhoneApplicationPageBackKeyPress(object sender, CancelEventArgs e)
		{
			if (_navHistory.Count <= 1)
			{
				return;
			}
			e.Cancel = true;
			_navHistory.RemoveAt(_navHistory.Count - 1);
		}
	}
}
